package com.example.da20;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import org.example.Easy2LearnApplication;
import org.example.domain.Manual;
import org.example.domain.Manual40;
import org.example.mapper.Manual40Mapper;
import org.example.mapper.ManualMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SpringBootTest(classes = {Easy2LearnApplication.class, TestConfig.class})
public class TxtReader {

    @Resource
    private ManualMapper manualMapper;

    @Resource
    private Manual40Mapper manual40Mapper;

    @SneakyThrows
    @Test
    public void test01(){
        String path = "C:\\Users\\SIM\\Downloads\\output.txt";
        int index = 0;
        try (BufferedReader br = new BufferedReader(new FileReader(path))) {
            String line;
            StringBuilder context = new StringBuilder();
            List<Manual40> list = new ArrayList<>();
            while ((line = br.readLine()) != null) {
                context.append(line);
                if (line.equals("------------------------")) {
//                    System.out.println(context);
                    Manual40 manual = getContext40(context);
                    if (manual!=null) {
                        manual.setAirplane("40");
                        list.add(manual);
                        manual40Mapper.insert(manual);
                        index++;
                        System.out.println("插入到第："+index+"项");
                    }
                    context = new StringBuilder();
                }
            }
        }
    }

    private Manual40 getContext40(StringBuilder context) {
        /**
         * 从context中获取 01-00-00 这种 两个数字-两个数字-两个数字的内容
         */
        String contextStr = context.toString();

        // 添加精简的调试信息
        boolean containsPage1 = contextStr.contains("Page 1"); // 移除空格要求
        boolean containsPage2 = contextStr.contains("Page 2");

        // 检查是否包含编码模式
        Pattern codePattern = Pattern.compile("\\d{2}-\\d{2}-\\d{2}");
        Matcher codeMatcher = codePattern.matcher(contextStr);
        boolean containsCode = codeMatcher.find();
        codeMatcher.reset(); // 重置matcher位置

        // 简化调试输出
//        System.out.println("文本内容: " + contextStr);
//        System.out.println("包含'Page 1': " + containsPage1);
//        System.out.println("包含'Page 2': " + containsPage2);
//        System.out.println("包含编码: " + containsCode);

        // 只检查页码和编码
        if ((containsPage1 || containsPage2) && containsCode) {
            Manual40 manual = new Manual40();
            // 获取最大编码
            String maxCode = getString(codePattern, contextStr);

            // 输出前两个数字最大的编号
            if (maxCode != null) {
//                System.out.println(contextStr);
                String substring = contextStr.substring(contextStr.indexOf("页内容：") + 4, contextStr.length() - 1);
                System.out.println(substring);
                manual.setContext(substring);
                System.out.println("编码: " + maxCode);
                manual.setIndex(maxCode);
                return manual;
            }
        }
        return null;
    }

    @Resource
    private OpenAiChat openAiChat;

    @Test
    public void getDa20Context(){
        openAiChat.setSystemPrompt("现在要你根据一段话从中提取出道问题，给你的是英文内容，回答我的是中文题目。题目是单选题，选项是A B C D。只能从给你的信息中提取问题，" +
                "题目中尽量给出上下文，我给你的信息是关于DA20-C1飞机的，所以尽量说明这是DA20-C1飞机，不要超出我给你的信息范围，题目形式统一。" +
                "都是：第一行题目，第二道第五行是四个选项，第六行答案，第七行解析(解析给出你引用的依据来源，这里可以用英文),不要有额外多余的标点符号");
        openAiChat.setAssistantRole("你是一个阅读理解大师，中英翻译高手和对飞机很了解的飞机维修人员。");
        openAiChat.setModel("doubao-1.5-vision-pro-250328");
        List<Manual> list = manualMapper.selectList(new LambdaQueryWrapper<Manual>().lt(Manual::getId, 1006));
        for (int i = 842; i < list.size(); i++) {
            Manual manual = list.get(i);
            String context = manual.getContext();
            String chat = openAiChat.chat(context);
            System.out.println(context);
            System.out.println("==========================================================");
            System.out.println(chat);
            manual.setQuestions(chat);
            manualMapper.updateById(manual);
        }
    }

    @Test
    public void getTest(){
        String question = "Words with Special Meanings\n" +
                "In this maintenance manual, the words that follow have special meanings:\n" +
                "Adjust: To put to a specified position or condition. For example, adjust the clearance to 1 mm.\n" +
                "Check: A technical name for a group of maintenance tasks.\n" +
                "For example, the 100 hour check\n" +
                "Examine: To look carefully at an item. It includes steps such as the following:\n" +
                "- Make sure that the item:\n" +
                "- is complete\n" +
                "- is correctly attached\n" +
                "- has no loose parts\n" +
                "- shows no signs of leaks\n" +
                "- is not cracked or damaged\n" +
                "- is not worn.\n" +
                "- Make sure that:\n" +
                "- the surface protection is not damaged\n" +
                "- all locking devices are installed correctly.\n" +
                "- Make sure that items such as pipes and cables:\n" +
                "- look serviceable\n" +
                "- do not rub against other items.\n" +
                "For log books and other technical records:\n" +
                "- to find outstanding faults\n" +
                "- to make sure they are up-to-date and correctly maintained.\n" +
                "Inspection: The procedure which compares an object with its standard or specification.\n" +
                "Measure: To find out the dimensions, capacity or quantity of something.\n" +
                "Monitor: To look at something during a time. For example, monitor the engine speed indicator.\n" +
                "Record: (1) Technical name for something that shows what was done.\n" +
                "For example, write the result of the test in the engine record.\n" +
                "(2) The act of making a record. For example, record the result of the test in the\n" +
                "aircraft log book.\n" +
                "Replace: To remove an unserviceable item and install a serviceable item in the same location. \n" +
                "A new part or new parts, as applicable, must be installed.\n" +
                "Set: To put equipment into a given adjustment, condition or mode.\n" +
                "For example, set the altimeter scale to 1013 mb or 29.92 in.Hg.\n" +
                "Task: An assigned work or a procedure. For example, each step of the task has an identifi\u0002cation letter.\n" +
                "Test: That which you do when you operate or examine an item to make sure that it agrees \n" +
                "with the applicable specifications. For example, disconnect the systems which are not \n" +
                "necessary for the test. Or do an engine test.";
        openAiChat.clearHistory();
        openAiChat.setSystemPrompt("现在要你根据一段话从中提取出4道问题，给你的是英文内容你要给我翻译成中文，回答我的是中文题目。题目是单选题，选项是A B C D。只能从给你的信息中提取问题，" +
                "题目中尽量给出上下文，我给你的信息是关于DA20-C1飞机的，所以尽量说明这是DA20-C1飞机，不要超出我给你的信息范围，题目形式统一。" +
                "都是：第一行题目，第二道第五行是四个选项，第六行答案，第七行解析(解析给出你引用的依据来源，这里可以用英文),不要有额外多余的标点符号");
        openAiChat.setAssistantRole("你是一个阅读理解大师，中英翻译高手和对飞机很了解的飞机维修人员。");
        openAiChat.setModel("doubao-1.5-vision-pro-250328");
        String chat = openAiChat.chat(question);
        System.out.println(chat);
    }

    public static Manual getContext(StringBuilder context){

        /**
         * 从context中获取 01-00-00 这种 两个数字-两个数字-两个数字的内容
         */
        String contextStr = context.toString();

        // 添加精简的调试信息
        boolean containsPage1 = contextStr.contains("Page 1"); // 移除空格要求
        boolean containsPage2 = contextStr.contains("Page 2");

        // 检查是否包含编码模式
        Pattern codePattern = Pattern.compile("\\d{2}-\\d{2}-\\d{2}");
        Matcher codeMatcher = codePattern.matcher(contextStr);
        boolean containsCode = codeMatcher.find();
        codeMatcher.reset(); // 重置matcher位置

        // 简化调试输出
//        System.out.println("文本内容: " + contextStr);
//        System.out.println("包含'Page 1': " + containsPage1);
//        System.out.println("包含'Page 2': " + containsPage2);
//        System.out.println("包含编码: " + containsCode);

        // 只检查页码和编码
        if ((containsPage1 || containsPage2) && containsCode) {
            Manual manual = new Manual();
            // 获取最大编码
            String maxCode = getString(codePattern, contextStr);

            // 输出前两个数字最大的编号
            if (maxCode != null) {
//                System.out.println(contextStr);
                String substring = contextStr.substring(contextStr.indexOf("页内容：") + 4, contextStr.length() - 1);
                System.out.println(substring);
                manual.setContext(substring);
                System.out.println("编码: " + maxCode);
                manual.setIndex(maxCode);
                return manual;
            }
        }
        return null;
    }

    private static String getString(Pattern pattern, String contextStr) {
        Matcher matcher = pattern.matcher(contextStr);

        String maxCode = null;
        int maxFirstPart = -1;

        // 查找所有匹配项
        while (matcher.find()) {
            String match = matcher.group();

            // 提取前两个数字并转换为整数
            String firstPart = match.substring(0, 2);
            int firstPartValue = Integer.parseInt(firstPart);

            // 比较并记录最大值
            if (firstPartValue > maxFirstPart) {
                maxFirstPart = firstPartValue;
                maxCode = match;
            }
        }
        return maxCode;
    }
}

